/**
 * 
 */
package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.rs.model.ZhGoods;
import com.rs.service.ZhGoodsService;
import com.rs.util.SpringContextUtils;













import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;




/**
 * @author zjw
 *
 */
@SuppressWarnings("serial")
public class GoodsUpdate extends JFrame {

	private JPanel contentPane;
	private JTextField txt_gname;
	private JTextField txt_g_used_num;
	private JTextField txt_gresiduenum;
	private JTextField txt_gnum;
	
	private Integer gid;
	ZhGoodsService goodservice = SpringContextUtils.getBean(ZhGoodsService.class);
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GoodsUpdate frame = new GoodsUpdate(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GoodsUpdate(final JDesktopPane jdesktoppane) {
		
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {

				GoodsList goodslist = new GoodsList(jdesktoppane);
				goodslist.setVisible(true);
				
				jdesktoppane.add(goodslist);
				
			}
			
		});

		setResizable(false);

		setTitle("修改设备信息");
		setBounds(100, 100, 578, 412);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel lb_gname = new JLabel("名      称：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_gname, 46, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_gname, 55, SpringLayout.WEST, contentPane);
		contentPane.add(lb_gname);
		
		txt_gname = new JTextField();
		txt_gname.setEditable(false);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_gname, 25, SpringLayout.EAST, lb_gname);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, txt_gname, -304, SpringLayout.SOUTH, contentPane);
		contentPane.add(txt_gname);
		txt_gname.setColumns(10);
		
		JLabel lb_gresiduenum = new JLabel("剩余数量：");
		contentPane.add(lb_gresiduenum);
		
		txt_gresiduenum = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_gresiduenum, -21, SpringLayout.WEST, txt_gresiduenum);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_gresiduenum, 379, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_gresiduenum, -67, SpringLayout.EAST, contentPane);
		contentPane.add(txt_gresiduenum);
		txt_gresiduenum.setColumns(10);
		
		JLabel lb_gusednum = new JLabel("已使用数量：");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, lb_gusednum, -242, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_gusednum, 0, SpringLayout.WEST, lb_gname);
		contentPane.add(lb_gusednum);
		
		txt_g_used_num = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_g_used_num, 49, SpringLayout.SOUTH, txt_gname);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_g_used_num, 25, SpringLayout.EAST, lb_gusednum);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_g_used_num, 0, SpringLayout.EAST, txt_gname);
		contentPane.add(txt_g_used_num);
		txt_g_used_num.setColumns(10);
		
		JLabel lb_gnum = new JLabel("数   量：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_gresiduenum, 55, SpringLayout.SOUTH, lb_gnum);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_gname, -40, SpringLayout.WEST, lb_gnum);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_gnum, 0, SpringLayout.NORTH, lb_gname);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_gnum, 0, SpringLayout.WEST, lb_gresiduenum);
		contentPane.add(lb_gnum);
		
		txt_gnum = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_gresiduenum, 49, SpringLayout.SOUTH, txt_gnum);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_gnum, 384, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_gnum, -67, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_gnum, -3, SpringLayout.NORTH, lb_gname);
		contentPane.add(txt_gnum);
		txt_gnum.setColumns(10);
		
		JButton btn_update = new JButton("修改");
		btn_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZhGoods goods = new ZhGoods();
				goods.setG_id(gid);
				goods.setG_num(Integer.parseInt(txt_gnum.getText()));
				goods.setG_residue_num(Integer.parseInt(txt_gresiduenum.getText()));
				goods.setG_used_num(Integer.parseInt(txt_g_used_num.getText()));
				goodservice.updateGoods(goods);
				JOptionPane.showMessageDialog(new JButton(), "修改成功！");
//				new GoodsList().setVisible(true);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_update, 105, SpringLayout.SOUTH, txt_g_used_num);
		sl_contentPane.putConstraint(SpringLayout.WEST, btn_update, 115, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_update, -325, SpringLayout.EAST, contentPane);
		contentPane.add(btn_update);
		
		JButton btn_reset = new JButton("重置");
		btn_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				txt_gname.setText("");
				txt_gnum.setText("");
				txt_g_used_num.setText("");
				txt_gresiduenum.setText("");
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_reset, 0, SpringLayout.NORTH, btn_update);
		sl_contentPane.putConstraint(SpringLayout.WEST, btn_reset, 104, SpringLayout.EAST, btn_update);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_reset, -116, SpringLayout.EAST, contentPane);
		contentPane.add(btn_reset);
		
	}
	
	public void select(Object id){
	
		ZhGoods goods = new ZhGoods();
		goods.setG_id(Integer.parseInt(id+""));
		goods.setPagenum(1);
		ZhGoods att = goodservice.selectGoods(goods).getList().get(0);
		txt_gname.setText(att.getG_name());
		txt_gnum.setText(att.getG_num()+"");
		txt_g_used_num.setText(att.getG_used_num()+"");
		txt_gresiduenum.setText(att.getG_residue_num()+"");
		gid = Integer.parseInt(id+"");
	}
}
